// pagesShangping/pages/address/index.js
const app = getApp();
import { baseUrl } from "../../../api/request";
import { getStorage } from "../../../utils/localStorage.js";
import { address_List, address_Delete } from "../../../api/shopping"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义导航栏参数
        navData: {
            title: '收货地址',
            level: 2 //导航栏级别  
        },
        listQuery: {
            pageSize: 10,
            pageNum: 1,
        },
        total: 0,
        baseUrl: baseUrl,
        userInfo: {},
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        pageBottom: app.globalData.bottomLift,
        addressList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({ userInfo: getStorage('user') });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            addressList: [],
        });
        this.getAddeessList();
    },

    //查询地址列表
    getAddeessList() {
        this.data.listQuery.userId = this.data.userInfo.id;
        address_List(this.data.listQuery).then(res => {
            this.setData({
                addressList: [...this.data.addressList, ...res.data.rows],
                total: Math.ceil(res.data.total / this.data.listQuery.pageSize)
            });
        });
    },

    //列表修改，删除按钮点击事件 (upload:修改，delete:删除)
    listOption(e) {
        const type = e.currentTarget.dataset.type, id = e.currentTarget.dataset.id;
        if (type === 'upload') {
            wx.navigateTo({
                url: '/pagesShangping/pages/addAddress/index?type=' + type + '&id=' + id,
            });
        } else {
            wx.showModal({
                title: '警告',
                content: '请确定是否要删除该条数据',
                showCancel: true,
                success: (res => {
                    if (res.confirm) {
                        address_Delete({ id: id }).then(res => {
                            wx.showToast({
                                icon: 'none',
                                title: res.data.msg,
                                duration: 2000
                            });
                            if (res.data.code === 200) {
                                setTimeout(() => {
                                    this.setData({
                                        addressList: [],
                                    });
                                    this.getAddeessList();
                                }, 2000)
                            };
                        });
                    };
                })
            });
            console.log('删除');
        };
    },

    //添加收获地址按钮点击事件
    addShAddress() {
        wx.navigateTo({
            url: '/pagesShangping/pages/addAddress/index?type=add',
        });
    },

    //列表点击事件
    listTap(e) {
        const item = e.currentTarget.dataset.item;
        let pages = getCurrentPages(), // 获取页面栈
            currPage = pages[pages.length - 1], // 当前页面
            prevPage = pages[pages.length - 2]; // 上一个页面
        prevPage.setData({
            // 要设置的值
            addressData:item
        });
        wx.navigateBack({ delta: 1 });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if ((this.data.listQuery.pageNum + 1) <= this.data.total) {
            this.setData({
                "listQuery.pageNum": this.data.listQuery.pageNum += 1
            });
            this.getAddeessList();
        };
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})